package com.kmvc.message.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.kmvc.message.R;
import com.kmvc.message.tools.WebService;
import com.kmvc.ui.IphoneTitleBarActivity;

public class getRegistKeyActivity extends IphoneTitleBarActivity{
	
	private Button next;
	private TextView PhoneNumber,time_meter;
	private String phonenumber;
	private Handler handler=new Handler();
	private String result;
	
	public boolean FromOther;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.get_regist_key);
		
		setTitle(R.string.getKey);
		setLeftButton(R.string.goback, null);
		
		next=(Button)findViewById(R.id.next);
		PhoneNumber=(TextView)findViewById(R.id.PhoneNumber);
		time_meter=(TextView)findViewById(R.id.time_meter);
		
		MessageApplication app=(MessageApplication)getRegistKeyActivity.this.getApplication();
		if(app.CansendMessage!=0){
			next.setEnabled(false);
			FromOther=true;
			time_meter.setText(app.CansendMessage+"秒后重新获取");
			handler.post(runnable);
		}
		
		time_meter.setVisibility(View.GONE);
		
		next.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View view) {
				phonenumber=PhoneNumber.getText().toString();
				if(CheckPhoneNumber()){
					new Thread(){
						public void run()
						{
							TelephonyManager mTelephonyMgr = (TelephonyManager)getRegistKeyActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
							String IMSI = mTelephonyMgr.getSubscriberId()+"";
							String MolNo = mTelephonyMgr.getDeviceId()+"";
							result=WebService.GetNewYanzhengCode(phonenumber,IMSI,MolNo);
							handler.post(run);
						}
					}.start();
					
				}else{
					AlertDialog.Builder builder = new AlertDialog.Builder(getRegistKeyActivity.this);
					builder.setCancelable(false);
					builder.setMessage("温馨提示:请输入正确的手机号");
					builder.setPositiveButton("确定",new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							PhoneNumber.setText("");
						}}).create().show();
				}
				
			}
			
		});
	}
	
	private boolean CheckPhoneNumber(){
		phonenumber=phonenumber.trim();
		if(phonenumber.length()==11){
			return true;
		}else{
			return false;
		}
	} 
	
	private Runnable run=new Runnable() {
		
		@Override
		public void run() {
			Toast.makeText(getRegistKeyActivity.this, result, Toast.LENGTH_LONG).show();
			if(result.contains("已发送")){
				MessageApplication app=(MessageApplication)getRegistKeyActivity.this.getApplication();
				app.CansendMessage=60;
				app.StartJishi();
				next.setEnabled(false);
				time_meter.setVisibility(View.VISIBLE);
				time_meter.setText("60秒后重新获取");
				handler.post(runnable);
			}
		}
	};
	
	
	Runnable runnable=new Runnable() {
		
		@Override
		public void run() 
		{
			MessageApplication app=(MessageApplication)getRegistKeyActivity.this.getApplication();
			if(app.CansendMessage!=0)
			{
				time_meter.setVisibility(View.VISIBLE);
				time_meter.setText(app.CansendMessage+"秒后重新获取");
				handler.postDelayed(runnable, 1000);
			}
			else 
			{
				 next.setEnabled(true);
				 time_meter.setText("");
				 time_meter.setVisibility(View.GONE);
			}
		}
	};
	
}
